<!--
  * 订单
  *
  * @Author:    syf
  * @Date:      2025-05-16 10:02:50
  * @Copyright  畅想科技
-->
<template>
  <a-modal
      :title="form.orderId ? '查看' : '添加'"
      :open="visibleFlag"
      @cancel="onClose"
      :maskClosable="false"
      :destroyOnClose="true"
  >
    <a-form ref="formRef" :model="form" :label-col="labelCol" :wrapper-col="wrapperCol" >
          <a-row>
                    <!-- <a-form-item label="订单编号"  name="orderId">
                      <a-input-number style="width: 100%" v-model:value="form.orderId" placeholder="订单编号" />
                    </a-form-item> -->
                    <!-- <a-form-item label="订单类型："  name="orderKind">
                      <a-input-number style="width: 100%" v-model:value="form.orderKind" placeholder="" />
                    </a-form-item> -->

                    <a-form-item label="订单类型" name="orderKind" class="smart-query-form-item">
                        <SmartEnumSelect enum-name="ORDERS_STATUS_ENUM" v-model:value="form.orderKind" width="160px" />
                    </a-form-item> 
                    <a-form-item label="用户昵称"  name="createId">
                      <a-input style="width: 100%" v-model:value="form.userId" placeholder="下单用户" />
                    </a-form-item>
                    <a-col :span="16">
                      
                    <a-form-item label="取货地址"  name="orderStart">
                      <place-tree v-model:value="form.orderStart" placeholder="请选择取货地址" />
                    </a-form-item>
                    </a-col>
                    <a-col :span="16">
                    <a-form-item label="配送地址"  name="orderEnd">
                      <place-tree v-model:value="form.orderEnd" placeholder="请选择配送地址" />
                    </a-form-item>
                    </a-col>
                    <a-col :span="16">
                    <a-form-item label="订单状态"  name="orderState">
                      <!-- <a-input style="width: 100%" v-model:value="form.orderState" placeholder="订单状态" /> -->
                    <a-select  v-model:value="form.orderState" placeholder="请选择订单状态">
                      <a-select-option v-for="item in typeList" :key="item.typeId" :title="item.typeName">{{ item.typeName }}</a-select-option>
                    </a-select>
                    </a-form-item>
                    </a-col>
                    <a-form-item label="截止时间"  name="deadTime">
                      <a-date-picker show-time valueFormat="YYYY-MM-DD HH:mm:ss" v-model:value="form.deadTime" style="width: 100%" placeholder="订单截止时间" />
                    </a-form-item>
                   
                    <a-col :span="16">
                    <a-form-item label="备注"  name="reMark">
                      <!-- <a-input style="width: 100%" v-model:value="form.reMark" placeholder="备注内容" /> -->
                      <textarea v-model="form.reMark" style="width: 100%; height: 100px; outline: none"></textarea>
                    </a-form-item>
                    </a-col>

          </a-row>

    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">关闭</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
  import { reactive, ref, nextTick } from 'vue';
  import _ from 'lodash';
  import { message } from 'ant-design-vue';
  import PlaceTree from '/@/components/support/place-tree/index.vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { ORDERS_STATUS_ENUM } from '/@/constants/support/orders-const';
  import SmartEnumSelect from '/@/components/framework/smart-enum-select/index.vue';
  import { orderApi } from '/@/api/business/order/order-api';
  import { smartSentry } from '/@/lib/smart-sentry';

  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList']);

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  function show(rowData) {
    Object.assign(form, formDefault);
    queryAll();
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData);
    }
    visibleFlag.value = true;
    nextTick(() => {
      formRef.value.clearValidate();
    });
  }

  function onClose() {
    Object.assign(form, formDefault);
    visibleFlag.value = false;
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const formDefault = {
              orderId: undefined, //订单编号
              orderKind: undefined, //订单类型：0:代取快递;1:代买物品;2:代拿外卖;3:代办事
              orderStart: undefined, //订单起点：跑腿路程起点
              orderEnd: undefined, //订单终点：跑腿路程终点
              orderState: '',//订单状态
              deadTime: undefined, //订单截止时间
              reMark: undefined, //备注内容
              createId: undefined, //订单创建人
  };

  let form = reactive({ ...formDefault });

  const rules = {
                  // orderId: [{ required: true, message: '订单编号 必填' }],
                  // orderKind: [{ required: true, message: '订单类型：0:代取快递;1:代买物品;2:代拿外卖;3:代办事 必填' }],
                  // orderStart: [{ required: true, message: '订单起点：跑腿路程起点 必填' }],
                  // orderEnd: [{ required: true, message: '订单终点：跑腿路程终点 必填' }],
                  // deadTime: [{ required: true, message: '订单截止时间 必填' }],
                  // beizhu: [{ required: true, message: '备注内容 必填' }],
                  // createId: [{ required: true, message: '订单创建人 必填' }],
  };
  const typeList = ref([]); //订单状态列表
  async function queryAll() {
    let res = await orderApi.queryType();
    // let res = await orderApi.queryEnum();
    typeList.value = res.data;
  }
  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields();
      save();
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show();
    try {
      if (form.orderId) {
        await orderApi.update(form);
      } else {
        await orderApi.add(form);
      }
      message.success('操作成功');
      emits('reloadList');
      onClose();
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      SmartLoading.hide();
    }
  }

  defineExpose({ 
    
    show,

  });
</script>
